﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;
using System.Windows.Markup;
using System.Xml;
using System.Windows.Controls;
using System.Globalization;

namespace MockupDesigner.AppItems.Services
{
    public class ConfigFileSerializationService
    {

        public IList<CustomToolbox> DeserializeToolboxesFromFile(string configPath)
        {
            IList<CustomToolbox> toolboxes = new List<CustomToolbox>();
            if (!File.Exists(configPath))
            {
                return toolboxes;
            }
            XElement root = XElement.Load(configPath);
            IEnumerable<XElement> toolboxesXML = root.Elements("Toolboxes").Elements("Toolbox");
            foreach (XElement toolboxXML in toolboxesXML)
            {
                CustomToolbox item = DeserializeToolbox(toolboxXML);
                if (item != null)
                    toolboxes.Add(item);
            }
            return toolboxes;
        }


        public CustomToolbox DeserializeToolbox(XElement toolboxXML)
        {
            CustomToolbox ctb = new CustomToolbox();
            ctb.ToolboxName = toolboxXML.Element("ToolboxName").Value;
            ctb.IsExpanded = Boolean.Parse(toolboxXML.Element("IsExpanded").Value);
            IEnumerable<XElement> itemsXML = toolboxXML.Elements("ToolboxItems").Elements("ToolboxItem");
            foreach (XElement itemXML in itemsXML)
            {
                ctb.Items.Add(DeserializeToolboxItem(itemXML));
            }
            return ctb;
        }


        public static ToolboxItem DeserializeToolboxItem(XElement toolboxItemXML)
        {
            ToolboxItem item = new ToolboxItem() { IsCustom = true };
            item.Width = Double.Parse(toolboxItemXML.Element("Width").Value, CultureInfo.InvariantCulture);
            item.Height = Double.Parse(toolboxItemXML.Element("Height").Value, CultureInfo.InvariantCulture);
            item.ToolTip = toolboxItemXML.Element("ToolTip").Value;
            item.Content = XamlReader.Load(XmlReader.Create(new StringReader(toolboxItemXML.Element("Content").Value)));
            return item;
        }


        public void SerializeToolboxesInFile(IList<CustomToolbox> toolboxes, string configPath)
        {
            XElement serializedItems = new XElement("Toolboxes",
                                   from item in toolboxes
                                   select new XElement("Toolbox",
                                              new XElement("ToolboxName", item.ToolboxName),
                                              new XElement("IsExpanded", item.IsExpanded),
                                              SerializeToolboxItems(item.Items.OfType<ToolboxItem>())
                                          ));
            XElement root = new XElement("Root");
            root.Add(serializedItems);
            root.Save(configPath);
        }

        public static XElement SerializeToolboxItems(IEnumerable<ToolboxItem> toolboxItems)
        {

            XElement serializedItems = new XElement("ToolboxItems",
                                       from item in toolboxItems
                                       let contentXaml = XamlWriter.Save(((ToolboxItem)item).Content)
                                       select new XElement("ToolboxItem",
                                                  new XElement("Width", item.Width),
                                                  new XElement("Height", item.Height),
                                                  new XElement("ToolTip",item.ToolTip),
                                                  new XElement("Content", contentXaml)
                                              )
                                   );
            return serializedItems;
        }

    }
}
